﻿using System;
using System.Windows.Forms;

namespace RSSReader
{
    public partial class RSSDashboard : Form
    {
        private const int COL_RSS_FEED_NAME = 0;
        private const int COL_RSS_FEED_URL = 1;

        private const int COL_TITLE_TO_MATCH_TITLE = 0;
        private const int COL_TITLE_TO_MATCH_IS_MOVIE = 1;
        private const int COL_TITLE_TO_MATCH_QUALITY = 2;

        public RSSDashboard()
        {
            InitializeComponent();
        }

        public void AddTitleRow(string title, bool isMovie, string quality)
        {
            string[] newRow = new string[] {title, (isMovie ? "Movie" : "Series"), quality};
            TitlesToMatch.Rows.Add(newRow);
        }

        public void AddFeedRow(string name, string url)
        {
            string[] newRow = new string[] {name, url};
            Feeds.Rows.Add(newRow);
        }

        private delegate void AddItemDelegate(object itemText);
        public void AddRssItem(object itemText)
        {
            if (Items.InvokeRequired)
            {
                AddItemDelegate addItemDelegate = AddRssItem;
                this.Invoke(addItemDelegate, itemText);
            }
            else
            {
                Items.Items.Add(itemText);
            }
        }

        private delegate void SetLastUpdateDateTimeDelegate(DateTime time);
        public void SetLastUpdateDateTime(DateTime time)
        {
            if (LastUpdateDateTime.InvokeRequired)
            {
                SetLastUpdateDateTimeDelegate setLastUpdateDateTimeDelegate = SetLastUpdateDateTime;
                this.Invoke(setLastUpdateDateTimeDelegate, time);
            }
            else
            {
                LastUpdateDateTime.Text = time.ToString("dd/MM/yyyy HH:mm:ss");
            }
        }

        private void OnFeedDeleteRow(object sender, DataGridViewRowsRemovedEventArgs e)
        {
            Program.DeleteRSSFeed(e.RowIndex);
        }

        private void OnTitleDeleteRow(object sender, DataGridViewRowsRemovedEventArgs e)
        {
            Program.DeleteTitle(e.RowIndex);
        }

        private void AddRSSFeed_Click(object sender, EventArgs e)
        {
            AddFeedRow(NewRSSName.Text, NewRSSUrl.Text);
            Program.AddRSSFeed(NewRSSName.Text, NewRSSUrl.Text);
        }

        private void AddRSSTitleToMatch_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(TitleToMatch.Text))
            {
                MessageBox.Show("Title cannot be empty");
                return;
            }

            AddTitleRow(TitleToMatch.Text, NewRSSIsMovie.Checked, TitleQuality.Text);
            Program.AddRSSTtitle(TitleToMatch.Text, NewRSSIsMovie.Checked, TitleQuality.Text);
        }

        private void ItemsDoubleClick(object sender, EventArgs e)
        {
            RSSItem item = Items.SelectedItem as RSSItem;
            Program.SearchItem(item, false);
        }

        private void UpdateNow_Click(object sender, EventArgs e)
        {
            UpdateNow.Enabled = false;
            Program.RunTimerCallback();
            UpdateNow.Enabled = true;
        }

        private delegate void SetIgnoreListDelegate(string ignoreList);
        public void SetIgnoreList(string ignoreList)
        {
            if (IgnoreList.InvokeRequired)
            {
                SetIgnoreListDelegate setIgnoreListDelegate = SetIgnoreList;
                this.Invoke(setIgnoreListDelegate, ignoreList);
            }
            else
            {
                IgnoreList.Text = ignoreList;
            }
        }

        private delegate void SetDropboxFilenameDelegate(string dropboxFilename);
        public void SetDropboxFilename(string dropboxFilename)
        {
            if (DropboxFilename.InvokeRequired)
            {
                SetDropboxFilenameDelegate setDropboxFilenameDelegate = SetDropboxFilename;
                this.Invoke(setDropboxFilenameDelegate, dropboxFilename);
            }
            else
            {
                DropboxFilename.Text = dropboxFilename;
            }
        }

        private void SetIgnoreListValue_Click(object sender, EventArgs e)
        {
            Program.SetIgnoreList(IgnoreList.Text);
        }

        private void SetDropboxFilenameButton_Click(object sender, EventArgs e)
        {
            Program.SetDropboxFilename(DropboxFilename.Text);
        }


        internal void SelectLastItem()
        {
            if (Items.Items.Count > 0)
                Items.SetSelected(Items.Items.Count - 1, true);
        }
    }
}
